<?php
require("../Vendor/twitteroauth/twitteroauth.php");
/**
 * Application level Controller
 *
 * This file is application-wide controller file. 

You can put all
 * application-wide controller-related methods here.
 *
 * PHP 5
 *
 * CakePHP

(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright 2005-2012, Cake 

Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * 

Redistributions of files must retain the above copyright notice.
 *
 * @copyright     

Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link     

     http://cakephp.org CakePHP(tm) Project
 * @package       app.Controller
 * @since        

 CakePHP(tm) v 0.2.9
 * @license       MIT License (http://www.opensource.org/licenses/mit-

license.php)
 */

App::uses('Controller', 'Controller');

/**
 * Application Controller
 *
 * Add 

your application-wide methods in the class below, your controllers
 * will inherit them.
 *
 * 

@package       app.Controller
 * @link http://book.cakephp.org/2.0/en/controllers.html#the-

app-controller
 */
class AppController extends Controller {

    public $helpers = array('Session','Facebook.Facebook' ,'Html', 'Form');
	var $components = array('Session'=>array('timeout'=>60), 'Facebook.Connect',
	'Auth' => array(
		'loginRedirect' => array('contoller'=>'posts','action'=>'fetchHome'),
		'logoutRedirect' => array('contoller'=>'User','action'=>'login'),
		'authError'=>"Access Denied",
		'authorize'=>'controller'
			));
	/**
     * this function gets the tattlr accounts from the db and sets them in the view before rendering the view.
     * @return void
     * @author "Omar Ruedy"
     */
			public function beforeRender() {
                  $currentAccount = "";
			$this->set('ad', $this->Auth->user('admin'));
        if ($this->action == 'login' || $this->action == 'fb_login' || $this->action == 'forgotpassword' || $this->action == 'logout' || ($this->params['controller'] == 'tattlraccounts' && $this->params['action'] == 'index')
                || ($this->params['controller'] == 'Users' && $this->params['action'] == 'add')
                || ($this->params['controller'] == 'Users' && $this->params['action'] == 'delete')
                || ($this->params['controller'] == 'Users' && $this->params['action'] == 'restore')
                || ($this->params['controller'] == 'tattlraccounts' && $this->params['action'] == 'addaccount')) {
            $this->set('accountsLeftBox', 'false');
        } else {
            $this->set('accountsLeftBox', 'true');
            $test1 = array();
            $images = array();
            if (($this->Auth->User('admin')) == 1) {
                $test = ClassRegistry::init('Account')->getAccountNamesAdmin();
                for ($i = 0; $i < count($test); $i++) {
                    $accID = ClassRegistry::init('Account')->getAccountIdByName($test[$i]['Account']['name']);
                    $result = (ClassRegistry::init('Account')->find('all', array('conditions' => array('Account.accID' => $accID['0']['Account']['accID']), 'fields' => 'Account.image')));
					$name = (ClassRegistry::init('Account')->find('all', array('conditions' => array('Account.accID' => $accID['0']['Account']['accID']), 'fields' => 'Account.name')));
                    if ($accID['0']['Account']['accID'] == $this->Auth->User('accID')) {
                        $currentAccount = $name['0']['Account']['name'];
                    }                   
				   if ($result[0]['Account']['image'] == null) {
                        $images[$i] = 'eggs.png';
                    } else {
                        $images[$i] = $result[0]['Account']['image'];
                    }
                }
                for ($i = 0; $i < count($test); $i++) {
                    $test1[$i] = $test[$i]['Account']['name'];
                }
            } else {
                $test = ClassRegistry::init('Account')->getAccountNames($this->Auth->user('userID'));
                for ($i = 0; $i < count($test); $i++) {
                    $accID = ClassRegistry::init('Account')->getAccountIdByName($test[$i]['accounts']['name']);
                    $result = (ClassRegistry::init('Account')->find('all', array('conditions' => array('Account.accID' => $accID['0']['Account']['accID']), 'fields' => 'Account.image')));
                    $name = (ClassRegistry::init('Account')->find('all', array('conditions' => array('Account.accID' => $accID['0']['Account']['accID']), 'fields' => 'Account.name')));
                    if ($accID['0']['Account']['accID'] == $this->Auth->User('accID')) {
                        $currentAccount = $name['0']['Account']['name'];
                    }
					if ($result[0]['Account']['image'] == null) {
                        $images[$i] = 'eggs.png';
                    } else {
                        $images[$i] = $result[0]['Account']['image'];
                    }
                }
                for ($i = 0; $i < count($test); $i++) {
                    $test1[$i] = $test[$i]['accounts']['name'];
                }
            }
			$this->set('currentlySelectedAccount', $currentAccount);
            $this->set('images', $images);
            $this->set('accounts', $test1);
        }
    }
		/*
		* Description of "isAuthorized"
		* @param  mixed $user
		*
		* The function checks if the given user is allowed to do a certain action(s) and returns true if user is allowed.
		* @return boolean
		* @author "Mohamed Elhawary"
		*/
		public function isAuthorized($user){
		// $this->set('admin', ClassRegistry::init('User')->isAdmin($this->Auth->user()));
			return true;
		}
	    /*
		* Description of "beforeFilter"
		*
		* The function is called and executed whenever any other function in the application is called to check if the action is allowed without logging in.
		*
		* @author "Mohamed Elhawary"
		*/
		public function beforeFilter(){
			$this->Auth->allow('login','forgotpassword','fb_login');
			
            if($this->action == 'login'){
				if($this->Auth->loggedIn()){
					$this->Session->setFlash('you are already logged in');
					$this->redirect(array('controller'=>'users','action'=>'index'));
				}
			}
		}
}